package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IAccountsDAO;
import com.AAL.Model.Account;
import com.AAL.Utils.HibernateUtil;

public class AccountsDAO implements IAccountsDAO {

	@Override
	public Account login(String username, String password) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Account account = null;
		try {
			account = (Account) (session
					.createQuery("from Account as acc where acc.userName=:username and acc.password=:password and acc.statusOfAccount.statusName='Active'")
					.setParameter("username", username)).setParameter(
					"password", password).uniqueResult();			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return account;
	}

	@Override
	public Account changePassword(Account account) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			int result = session
					.createQuery(
							"update Account as acc set acc.password=:password where acc.accountID=:accountID")
					.setParameter("accountID", account.getAccountID())
					.setParameter("password", account.getPassword())
					.executeUpdate();
			if (result == 0) {
				return null;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return account;
	}

	@Override
	public List<Account> getLstAccount() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account saveAndUpdateAccount(Account account) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(account);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return account;
	}

	@Override
	public Account getAccount(int accountID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Account account = null; // (Account) session.load(Account.class,
								// accountID);
		account = (Account) session
				.createQuery(" from Account where accountID=:accountID")
				.setParameter("accountID", accountID).uniqueResult();
		session.getTransaction().commit();
		return account;
	}

	@Override
	public Account deleteAccount(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Account editAccountOfDealer(Account account) {
		return null;
	}

	@Override
	public Account editAccountOfEmployee(Account account) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			int result = session
					.createQuery(
							"update Account as acc set acc.password=:password where acc.accountID=:accountID")
					.setParameter("accountID", account.getAccountID())
					.setParameter("password", account.getPassword())
					.executeUpdate();
			if (result == 0) {
				return null;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return account;
	}

	@Override
	public Account checkUserName(String username) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Account account = null;
		try {
			account = (Account) (session
					.createQuery("from Account as acc where acc.userName=:username")
					.setParameter("username", username)).uniqueResult();			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return account;
	}

	@Override
	public Account checkEmail(String email) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Account account = null;
		try {
			account = (Account) (session
					.createQuery("from Account as acc where acc.accountInformation.email=:email")
					.setParameter("email", email)).uniqueResult();			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return account;
	}


}
